<template>
  <div class="custom-body" :style="borderStyle">
    <div v-if="showHeader" class="custom-header" :style="titleStyle">
      <div class="header-body">
        <div v-if="temp.sfxs === true" :style="titleFont" class="header-icon">
          <i :class="temp.lmtb" />
        </div>
        <div class="header-title">
          <div
            v-for="(item, index) in columns"
            :key="'item-' + index"
            class="custom-header-li"
            :style="index === active ? activeStyle : ''"
            @click="headerClick(item, index)"
            @mouseenter="mouseenter(index)"
            @mouseleave="mouseleave(index)"
          >
            <span>{{ item.label }}</span>
          </div>
        </div>
        <div
          v-if="isMore == true"
          :style="titleFont"
          class="header-icon"
          @click="callBack"
        >
          更多
        </div>
      </div>
    </div>
    <div class="custom-content">
      <component
        :is="template"
        v-if="template"
        :show-header="false"
        :layout="layout"
        :temp="column"
        @fetch-data="callBack"
        @fetch-entity="callBackEntity"
      />
    </div>
  </div>
</template>
<script>
import { queryEntity, queryGuestEntity } from '@/api/portal/manager/columnTemp'
import { getToken } from '@/utils/auth'
export default {
  name: 'Portal4PcTabsTempV1',
  props: {
    temp: {
      type: Object,
      default() {
        return {}
      }
    },
    showHeader: {
      type: Boolean,
      default: true
    },
    layout: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      columns: [],
      active: 0,
      activeId: null,
      template: null,
      column: {},
      isMore: false,
      customStyle: {},
      titleStyle: {},
      bodyStyle: {},
      titleFont: {},
      activeStyle: {},
      borderStyle: {},
      activeTow: null
    }
  },
  computed: {
    themes() {
      return this.$store.getters.theme
    }
  },
  watch: {
    layout: {
      handler(newData, oldData) {
        this.customStyle = newData
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    },
    temp: {
      handler(newData, oldData) {
        if (this.showHeader === true) {
          const pzx = JSON.parse(newData.data.pzx)
          this.columns = pzx.data
          this.activeId = this.columns[this.active].temp
        } else {
          const pzx = JSON.parse(newData.pzx)
          this.columns = pzx.data
          this.activeId = this.columns[this.active].temp
        }
        this.initEntity()
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    },
    themes: {
      handler(val, oldVal) {
        this.borderStyle.borderRadius = val.bkdx
        if (this.temp.sfxsbk === true) {
          this.borderStyle.border = '1px solid ' + val.bkys
        }
        if (this.temp.bkyy === true) {
          this.borderStyle.boxShadow = '0 2px 4px rgba(0, 0, 0, .12)'
        }
        this.borderStyle.background = val.columnGroundColor
        this.borderStyle.color = val.fontColor
        this.titleStyle.background = val.columnTitleGroundColor
        this.titleStyle.color = val.columnTitleFontColor
        this.activeStyle.fontWeight = 500
        this.activeStyle.borderBottom =
          '3px solid ' + val.changeBackGroundColor
        this.activeStyle = Object.assign({}, this.activeStyle)
        this.titleStyle = Object.assign({}, this.titleStyle)
        this.borderStyle = Object.assign({}, this.borderStyle)
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    headerClick(item, index) {
      this.active = index
      this.activeId = item.temp
      this.initEntity()
    },
    initEntity() {
      if (this.activeId !== null) {
        if (getToken()) {
          queryEntity({ id: this.activeId }).then(res => {
            const { data } = res
            const path = data.mbdl + '/' + data.mbfl + '/' + data.mb
            this.template = () => import(`@/template/${path}`)
            this.column = data
            this.isMore = this.columns[this.active].isMore
            this.column.dkfs = this.columns[this.active].dkfs
            this.column.path = this.columns[this.active].path
            this.column.showHeader = true
            this.column.menuId = this.temp.column.menu.id
            this.column.menuName = this.temp.column.menu.name
            this.column.columnName = this.columns[this.active].label
          })
        } else {
          queryGuestEntity({ id: this.activeId }).then(res => {
            const { data } = res
            const path = data.mbdl + '/' + data.mbfl + '/' + data.mb
            this.template = () => import(`@/template/${path}`)
            this.column = data
            this.isMore = this.columns[this.active].isMore
            this.column.dkfs = this.columns[this.active].dkfs
            this.column.path = this.columns[this.active].path
            this.column.showHeader = true
            this.column.menuId = this.temp.column.menu.id
            this.column.menuName = this.temp.column.menu.name
            this.column.columnName = this.columns[this.active].label
          })
        }
      }
    },
    callBackEntity(data) {
      data.column = this.column
      this.$emit('fetch-entity', data)
    },
    callBack() {
      if (this.column.dkfs === 2) {
        // 自定义打开方式
        if (undefined !== this.column.path && this.column.path !== null) {
          window.open(this.column.path, '_blank')
        }
      } else if (this.column.dkfs === 1) {
        this.$emit('fetch-data', this.column)
      } else {
        console.log('')
      }
    },
    mouseenter(index) {
      // this.activeTow = index
    },
    mouseleave(index) {
      // this.activeTow = null
    }
  }
}
</script>

<style scoped>
.custom-body {
  width: 100%;
  height: 100%;
  position: relative;
  display: flex;
  flex-flow: column nowrap;
  border-radius: 4px;
  box-sizing: border-box;
  overflow: hidden;
}
.custom-header {
  width: 100%;
  height: 40px;
  flex: 0 1 auto;
  padding: 0px 10px;
  box-sizing: border-box;
  line-height: 40px;
  cursor: pointer;
}
.header-body {
  width: 100%;
  height: 41px;
  display: flex;
  flex-flow: row nowrap;
}
.header-icon {
  width: 40px;
  height: 40px;
  flex: 0 1 auto;
  text-align: center;
}
.header-title {
  height: 50px;
  flex: 1 1 auto;
  display: flex;
  flex-flow: row nowrap;
}
.custom-header-li {
  padding: 0px 10px;
  box-sizing: border-box;
  flex: 0 1 auto;
  height: 40px;
  position: relative;
}
.custom-header-l span {
  display: block;
  height: 100%;
  position: relative;
}
.custom-content {
  width: 100%;
  height: auto;
  flex: 1 1 auto;
  overflow: hidden;
  box-sizing: border-box;
}
</style>
